package src.first;

import java.lang.annotation.Annotation;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;

public class ClassDeclarationViewer {

    public static void main(String[] args) {
        try {
            Class<?> aClass = Class.forName("java.util.ArrayList");

            System.out.println("类的标准名称："+aClass.getCanonicalName());
            System.out.println("类的修饰符："+ Modifier.toString(aClass.getModifiers()));

            //输出类的泛型参数
            TypeVariable<? extends Class<?>>[] typeParameters = aClass.getTypeParameters();
            System.out.println("类的泛型参数：");
            if(typeParameters.length != 0){
                for (TypeVariable<? extends Class<?>> typeParameter : typeParameters) {
                    System.out.println(typeParameter.getName()+"\t");
                }
            }else {
                System.out.println("空");
            }
            //输出类所有实现的接口
            Type[] interfaces = aClass.getGenericInterfaces();
            System.out.println("类所实现的接口：");
            if(interfaces.length != 0){
                for (Type anInterface : interfaces) {
                    System.out.println("\t"+anInterface);
                }
            }else {
                System.out.println("空");
            }
            //输出类的直接继承类，如果是继承自Object则返回空
            Type superclass = aClass.getGenericSuperclass();
            System.out.println("类的直接继承类：");
            if(superclass != null){
                System.out.println(superclass);
            }else {
                System.out.println("空");
            }

            //输出类的所有注释信息，有些注释信息是不能通过反射获取的
            Annotation[] annotations = aClass.getAnnotations();
            System.out.println("类的注解：");
            if(annotations.length != 0){
                for (Annotation annotation : annotations) {
                    System.out.println("\t"+annotation);
                }
            }else {
                System.out.println("空");
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
